/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						       dialog_creature_portal.cpp

	$Header: /game/recruit_dialog.cpp $

	$NoKeywords: $

 ************************************************************************/
// kill the 255+ character identifier warning.  Templates tend to exceed that easily.
#include "precompiled.h"
#pragma warning( disable: 4786 )

#include "adaptor_handler.h"
#include "bitmap_group.h"
#include "bitmap_group_cache.h"
#include "bound_handler.h"
#include "building_traits.h"
#include "create_creature_button.h"
#include "creature_traits.h"
#include "creature_type.h"
#include "dialog_creature_portal.h"
#include "elements_of.h"
#include "format_string.h"
#include "town.h"

static t_bitmap_group_cache const k_layout( "dialog.creature_portal" );
extern t_bitmap_group_cache const k_creature_select_bitmaps;

static int get_discount_price( t_creature_type creature, int discount )
{
	int result = get_traits( creature ).cost[k_gold];

	return (result * (100 - discount) + 50) / 100;
}

// ---------------------------------------------------------------------
// creature portal dialog
// ---------------------------------------------------------------------
t_dialog_creature_portal::t_dialog_creature_portal( t_window* parent, t_town* town, 
		                                            t_material_array const& budget,
													int discount )
						: t_recruit_dialog( parent, town->get_portal_creature(),
						                    town->get_portal_population(),
											budget, k_layout.get(),
											get_discount_price( town->get_portal_creature(),
											                    discount ))
{
	m_ring_bitmaps = k_creature_select_bitmaps.get();
	m_town = town;
	m_discount = discount;

	t_toggle_button* button;
	int				 i;
	std::string      layer_name;
	t_screen_point   point;
	t_button_handler handler;
	std::vector<t_creature_type> const& creature_types = get_portal_creature_types();

	for (i = 0; i < (int)creature_types.size(); i++)
	{
		layer_name = format_string( "ring_%i", i + 1 );
		point = m_layout->find( layer_name )->get_rect().top_left();
		button = new t_toggle_button( point, this, true );
		create_creature_button( button, m_ring_bitmaps,
			                    get_creature_portrait( creature_types[i], 52 ), "" );
		button->set_pressed( creature_types[i] == town->get_portal_creature() );
		button->set_help_balloon_text( get_creature_name( creature_types[i], 2, false ));
		button->set_right_click_text( get_traits( creature_types[i] ).help_text );
		handler = add_2nd_argument( bound_handler( *this,
			      &t_dialog_creature_portal::select_creature ), creature_types[i] );
		button->set_click_handler( handler );
		m_button_group.add( button );
	}
}

// ---------------------------------------------------------------------
// creature portal dialog
// ---------------------------------------------------------------------
void t_dialog_creature_portal::select_creature( t_button* button, t_creature_type creature )
{
	if (!button->is_pressed())
		return;

	m_town->set_portal_creature( creature );
	set_creature( creature, m_town->get_portal_population(), 
				  get_discount_price( creature, m_discount ));
}
